package com.qijian.maindata.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qijian.common.annotation.Log;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.core.page.TableDataInfo;
import com.qijian.common.enums.BusinessType;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.qijian.common.utils.poi.ExcelUtil;
import com.qijian.maindata.domain.ProductAttributeUnit;
import com.qijian.maindata.domain.query.ProductAttributeUnitQuery;
import com.qijian.maindata.service.IProductAttributeUnitService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品属性单位Controller
 *
 * @author ruoyi
 * @date 2021-08-18
 */
@Api(tags = "产品属性单位")
@RestController
@RequiredArgsConstructor
@RequestMapping("/business/unit")
public class ProductAttributeUnitController extends BaseController {
    private final IProductAttributeUnitService productAttributeUnitService;

    /**
     * 查询产品属性单位列表
     */
    @PreAuthorize("@ss.hasPermi('business:unit:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductAttributeUnitQuery query, Page<ProductAttributeUnit> page) {
        return getDataTable(productAttributeUnitService.page(Page.of(page.getCurrent(), page.getSize()), WhereEntityTool.invoke(query)));
    }

    @PreAuthorize("@ss.hasPermi('business:unit:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll(ProductAttributeUnitQuery query) {
        List<ProductAttributeUnit> data = productAttributeUnitService.list(WhereEntityTool.invoke(query));
        return getDataTable(data);
    }

    /**
     * 导出产品属性单位列表
     */
    @PreAuthorize("@ss.hasPermi('business:unit:export')")
    @Log(title = "产品属性单位", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ProductAttributeUnitQuery query) {
        List<ProductAttributeUnit> list = productAttributeUnitService.list(WhereEntityTool.invoke(query));
        ExcelUtil<ProductAttributeUnit> util = new ExcelUtil<>(ProductAttributeUnit.class);
        return util.exportExcel(list, "产品属性单位数据");
    }

    /**
     * 获取产品属性单位详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:unit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(productAttributeUnitService.getById(id));
    }

    /**
     * 新增产品属性单位
     */
    @PreAuthorize("@ss.hasPermi('business:unit:add')")
    @Log(title = "产品属性单位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductAttributeUnit productAttributeUnit) {
        return toAjax(productAttributeUnitService.save(productAttributeUnit));
    }

    /**
     * 修改产品属性单位
     */
    @PreAuthorize("@ss.hasPermi('business:unit:edit')")
    @Log(title = "产品属性单位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductAttributeUnit productAttributeUnit) {
        return toAjax(productAttributeUnitService.updateById(productAttributeUnit));
    }

    /**
     * 删除产品属性单位
     */
    @PreAuthorize("@ss.hasPermi('business:unit:remove')")
    @Log(title = "产品属性单位", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(productAttributeUnitService.removeByIds(ids));
    }
}
